home *** CD-ROM | disk | FTP | other *** search
/ Reverse Code Engineering RCE CD +sandman 2000 / ReverseCodeEngineeringRceCdsandman2000.iso / RCE / Ebooks / Thinking in Java / c14 / BangBean2.java next >
Encoding:
Java Source  |  2000-05-25  |  6.0 KB  |  182 lines

  1. //: BangBean2.java
  2. //////////////////////////////////////////////////
  3. // Copyright (c) Bruce Eckel, 1998
  4. // Source code file from the book "Thinking in Java"
  5. // All rights reserved EXCEPT as allowed by the
  6. // following statements: You can freely use this file
  7. // for your own work (personal or commercial),
  8. // including modifications and distribution in
  9. // executable form only. Permission is granted to use
  10. // this file in classroom situations, including its
  11. // use in presentation materials, as long as the book
  12. // "Thinking in Java" is cited as the source. 
  13. // Except in classroom situations, you cannot copy
  14. // and distribute this code; instead, the sole
  15. // distribution point is http://www.BruceEckel.com 
  16. // (and official mirror sites) where it is
  17. // freely available. You cannot remove this
  18. // copyright and notice. You cannot distribute
  19. // modified versions of the source code in this
  20. // package. You cannot use this file in printed
  21. // media without the express permission of the
  22. // author. Bruce Eckel makes no representation about
  23. // the suitability of this software for any purpose.
  24. // It is provided "as is" without express or implied
  25. // warranty of any kind, including any implied
  26. // warranty of merchantability, fitness for a
  27. // particular purpose or non-infringement. The entire
  28. // risk as to the quality and performance of the
  29. // software is with you. Bruce Eckel and the
  30. // publisher shall not be liable for any damages
  31. // suffered by you or any third party as a result of
  32. // using or distributing software. In no event will
  33. // Bruce Eckel or the publisher be liable for any
  34. // lost revenue, profit, or data, or for direct,
  35. // indirect, special, consequential, incidental, or
  36. // punitive damages, however caused and regardless of
  37. // the theory of liability, arising out of the use of
  38. // or inability to use software, even if Bruce Eckel
  39. // and the publisher have been advised of the
  40. // possibility of such damages. Should the software
  41. // prove defective, you assume the cost of all
  42. // necessary servicing, repair, or correction. If you
  43. // think you've found an error, please email all
  44. // modified files with clearly commented changes to:
  45. // Bruce@EckelObjects.com. (Please use the same
  46. // address for non-code errors found in the book.)
  47. /////////////////////////////////////////////////
  48.  
  49. // You should write your Beans this way so they 
  50. // can run in a multithreaded environment.
  51. import java.awt.*;
  52. import java.awt.event.*;
  53. import java.util.*;
  54. import java.io.*;
  55.  
  56. public class BangBean2 extends Canvas 
  57.     implements Serializable {
  58.   private int xm, ym;
  59.   private int cSize = 20; // Circle size
  60.   private String text = "Bang!";
  61.   private int fontSize = 48;
  62.   private Color tColor = Color.red;
  63.   private Vector actionListeners = new Vector();
  64.   public BangBean2() {
  65.     addMouseListener(new ML());
  66.     addMouseMotionListener(new MM());
  67.   }
  68.   public synchronized int getCircleSize() { 
  69.     return cSize; 
  70.   }
  71.   public synchronized void 
  72.   setCircleSize(int newSize) {
  73.     cSize = newSize;
  74.   }
  75.   public synchronized String getBangText() { 
  76.     return text; 
  77.   }
  78.   public synchronized void 
  79.   setBangText(String newText) {
  80.     text = newText;
  81.   }
  82.   public synchronized int getFontSize() { 
  83.     return fontSize; 
  84.   }
  85.   public synchronized void 
  86.   setFontSize(int newSize) {
  87.     fontSize = newSize;
  88.   }
  89.   public synchronized Color getTextColor() {
  90.     return tColor; 
  91.   }
  92.   public synchronized void 
  93.   setTextColor(Color newColor) {
  94.     tColor = newColor;
  95.   }
  96.   public void paint(Graphics g) {
  97.     g.setColor(Color.black);
  98.     g.drawOval(xm - cSize/2, ym - cSize/2, 
  99.       cSize, cSize);
  100.   }
  101.   // This is a multicast listener, which is
  102.   // more typically used than the unicast
  103.   // approach taken in BangBean.java:
  104.   public synchronized void addActionListener (
  105.       ActionListener l) {
  106.     actionListeners.addElement(l);
  107.   }
  108.   public synchronized void removeActionListener(
  109.       ActionListener l) {
  110.     actionListeners.removeElement(l);
  111.   }
  112.   // Notice this isn't synchronized:
  113.   public void notifyListeners() {
  114.     ActionEvent a =
  115.       new ActionEvent(BangBean2.this,
  116.         ActionEvent.ACTION_PERFORMED, null);
  117.     Vector lv = null;
  118.     // Make a copy of the vector in case someone
  119.     // adds a listener while we're 
  120.     // calling listeners:
  121.     synchronized(this) {
  122.       lv = (Vector)actionListeners.clone();
  123.     }
  124.     // Call all the listener methods:
  125.     for(int i = 0; i < lv.size(); i++) {
  126.       ActionListener al = 
  127.         (ActionListener)lv.elementAt(i);
  128.       al.actionPerformed(a);
  129.     }
  130.   }
  131.   class ML extends MouseAdapter {
  132.     public void mousePressed(MouseEvent e) {
  133.       Graphics g = getGraphics();
  134.       g.setColor(tColor);
  135.       g.setFont(
  136.         new Font(
  137.           "TimesRoman", Font.BOLD, fontSize));
  138.       int width = 
  139.         g.getFontMetrics().stringWidth(text);
  140.       g.drawString(text, 
  141.         (getSize().width - width) /2,
  142.         getSize().height/2);
  143.       g.dispose();
  144.       notifyListeners();
  145.     }
  146.   }
  147.   class MM extends MouseMotionAdapter {
  148.     public void mouseMoved(MouseEvent e) {
  149.       xm = e.getX();
  150.       ym = e.getY();
  151.       repaint();
  152.     }
  153.   }
  154.   // Testing the BangBean2:
  155.   public static void main(String[] args) {
  156.     BangBean2 bb = new BangBean2();
  157.     bb.addActionListener(new ActionListener() {
  158.       public void actionPerformed(ActionEvent e){
  159.         System.out.println("ActionEvent" + e);
  160.       }
  161.     });
  162.     bb.addActionListener(new ActionListener() {
  163.       public void actionPerformed(ActionEvent e){
  164.         System.out.println("BangBean2 action");
  165.       }
  166.     });
  167.     bb.addActionListener(new ActionListener() {
  168.       public void actionPerformed(ActionEvent e){
  169.         System.out.println("More action");
  170.       }
  171.     });
  172.     Frame aFrame = new Frame("BangBean2 Test");
  173.     aFrame.addWindowListener(new WindowAdapter(){
  174.       public void windowClosing(WindowEvent e) {
  175.         System.exit(0);
  176.       }
  177.     });
  178.     aFrame.add(bb, BorderLayout.CENTER);
  179.     aFrame.setSize(300,300);
  180.     aFrame.setVisible(true);
  181.   }
  182. } ///:~